<?php
class IndexAction extends BaseAction {
    public function index(){
    	$art = M('Article');
    	//获取最新五条头条新闻
    	$topNews = $art->field('id,title,info')
    					->where('attr like "%头条%"')
    					->order('date desc')
    					->limit(5)
    					->select();
    	//截取标题和文章内容简介
    	if(!empty($topNews)){
    		getSubStr($topNews[0]['info'], null, 80, 'utf-8');
    		getSubStr($topNews[0]['title'], null, 18, 'utf-8');
    		getSubStr($topNews, 'title', 9, 'utf-8',1);
    	}
		//格式化第二至五条头条新闻
    	foreach ($topNews as $key => $val){
    		$topNews[$key]['line'] = 0 == $key % 2 ? ' ' : '| ';
    	}
    	$this->assign('topNews',$topNews);
    	
    	//获取最新十篇新闻
    	$topTenNews = $art->field('id,title,date')
    					->limit(10)
    					->order('date desc')
    					->select();
    	getSubStr($topTenNews, 'title', 30, 'utf-8');
    	$this->assign('topTenNews',$topTenNews);
    	
    	//获取七篇推荐文章
    	$rec = $art->field('id,title,date')
    				->limit(7)
    				->where('attr like "%推荐%"')
    				->order('date desc')
    				->select();
    	getSubStr($rec, 'title', 15, 'utf-8');
    	$this->assign('rec',$rec);
    	
    	//获得四个顶级分类导航
    	$nav = M('Category');
    	$topFourNav = $nav->field('id,category_name')
    						->where('pid = 0')
    						->order('sort,id')
    						->limit(4)
    						->select();
//     	echo $nav->getLastSql();
//     	dump($topFourNav);
    	//设置显示样式
    	if($topFourNav){
    		foreach ($topFourNav as $key => $val){
				$topFourNav[$key]['class'] = 0 != $key % 2 ? 'list right bottom' : 'list bottom';
    			//查找该顶级分类菜单下及其子菜单下的所有文章
    			//获取所有子分类id sql语句：
// 				SELECT id
// 				FROM zgcms_category
// 				WHERE pid =45
// 				OR path LIKE  "%-45-%"
// 				LIMIT 0 , 30
    			$navArt = $art->field('zgcms_article.id as id,title,date')
    							->join('zgcms_category on category = zgcms_category.id')
    							->where('category in '.'(select id from zgcms_category where id = '.$val['id'].' or pid = '.$val['id'].' or path like "%-'.$val['id'].'-%")')
    							->order('date desc')
    							->limit(11)
    							->select();
    			getSubStr($navArt, 'title', 18, 'utf-8');
//     			echo $art->getLastSql();
//     			dump($navArt);
				$topFourNav[$key]['art'] = $navArt;
    		}
    	}
//     	dump($topFourNav);
    	$this->assign('fourNav',$topFourNav);
    	
    	//获取本月七篇热点文章
    	$hotNews = $art->field('id,title,date')
    					->where("DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d')")
    					->limit(7)
    					->order('count desc,date desc')
    					->select();
    	getSubStr($hotNews, 'title', 13, 'utf-8');
    	$this->assign('hotNews',$hotNews);
    	
    	//获取本月七篇回复总数最多的文章
    	$hotComArt = $art->field('id,title,date')
    					->where("DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d')")
    					->limit(7)
    					->order("(select count(*) from zgcms_comment where aid = zgcms_article.id and state = 1) desc")
    					->select();
    	getSubStr($hotComArt, 'title', 13, 'utf-8');
    	$this->assign('hotComArt',$hotComArt);
    	
    	//最近登录会员信息
    	$user = M('User');
    	$lastLogin = $user->field('id,user,face')
    					->limit(6)
    					->order('login_time desc,id')
    					->select();
    	$this->assign('lastLogin',$lastLogin);
    	
    	//获取最新图文资讯 4条
    	$picNews = $art->field('id,title,date,thumbnail')
    					->where('thumbnail <> \'\'')
    					->order('date desc')
    					->limit(4)
    					->select();
    	getSubStr($picNews, 'title', 20, 'utf-8');
    	$this->assign('picNews',$picNews);
    	
		//显示投票
		$vote = M('VoteSubject');
		$voteSub = $vote->field('zgcms_vote_subject.id,zgcms_vote_subject.title,sum(count) count')
						->join('zgcms_vote_item on zgcms_vote_subject.id = zgcms_vote_item.sid')
						->where('state = 1')
						->group('zgcms_vote_subject.id')
						->find();
		$this->assign('voteSub',$voteSub);
		$voteItem = M('VoteItem');
		$voteItemData = $voteItem->field('id,title')
								->where('sid = '.$voteSub['id'])
								->select();
		$this->assign('voteItemData',$voteItemData);
    	$this->display();
    }
    
}